﻿global using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace WXWorkSvc.Common.TagHelpers;
[HtmlTargetElement("desk-input-items", TagStructure = TagStructure.WithoutEndTag)]
public class DeskInputItemsTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    private readonly IHtmlGenerator _HtmlGenerator;

    public DeskInputItemsTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }
    [HtmlAttributeName("asp-rowno")]
    public int? RowNo { get; set; }
    [HtmlAttributeName("asp-Items")]
    public ModelExpression Items { get; set; } = default!;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> Fors { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-select-")]
    public Dictionary<string, IEnumerable<SelectListItem>> Selects { get; set; } = new();

    [HtmlAttributeName(DictionaryAttributePrefix = "asp-readonly-")]
    public Dictionary<string, string> ReadOnlys { get; set; } = new();
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {


        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("name", "自动创建");

        var tagValidationSummary = _HtmlGenerator.GenerateValidationSummary(ViewContext);
        if (tagValidationSummary != null)
            output.Content.AppendHtml(tagValidationSummary);

        var tagTable = new TagBuilder("table");
        tagTable.AddCssClass("table table-striped align-middle table-hover");
        output.Content.AppendHtml(tagTable);

        var tagHead = new TagBuilder("thead");
        tagTable.InnerHtml.AppendHtml(tagHead);
        var tagHeadRow = new TagBuilder("tr");
        tagHead.InnerHtml.SetHtmlContent(tagHeadRow);
        if (RowNo is not null)
            tagHeadRow.InnerHtml.SetHtmlContent("<th>#</th>");
        foreach (var f in Fors.Values)
        {
            var tagSpan = new TagBuilder("span");
            tagSpan.AddHtmlTooltip(f.Metadata);
            var tagTh = new TagBuilder("th");
            tagTh.InnerHtml.SetHtmlContent(tagSpan);
            tagHeadRow.InnerHtml.AppendHtml(tagTh);
        }

        var tagBody = new TagBuilder("tbody");
        tagTable.InnerHtml.AppendHtml(tagBody);

        var items = (IList)Items.Model;
        for (int i = 0; i < items.Count; i++)
        {
            var tagTr = new TagBuilder("tr");
            tagBody.InnerHtml.AppendHtml(tagTr);
            var itemModelExplorer = Items.ModelExplorer.GetExplorerForModel(items[i]);
            foreach (var f in Hiddens.Values)
            {
                var p = itemModelExplorer.GetExplorerForProperty(f.Metadata.Name);
                var expression = new ModelExpression($"{Items.Name}[{i}].{f.Metadata.Name}", p);
                var tagHidden = _HtmlGenerator.GenerateHidden(ViewContext, expression.ModelExplorer, expression.Name, expression.Model, false, null);
                tagTr.InnerHtml.AppendHtml(tagHidden);
            }
            if (RowNo is not null)
                tagTr.InnerHtml.AppendHtml($"<td>{RowNo++}</td>");

            foreach (var kv in Fors)
            {
                var p = itemModelExplorer.GetExplorerForProperty(kv.Value.Metadata.Name);
                var tagTd = new TagBuilder("td");
                tagTr.InnerHtml.AppendHtml(tagTd);

                if (ReadOnlys.ContainsKey(kv.Key))
                {
                    tagTd.InnerHtml.SetHtmlContent(p.Model?.ToString() ?? "");
                }
                else if (kv.Value.Metadata.ModelType.Name == "Boolean")
                {
                    var expression = new ModelExpression($"Items[{i}].{kv.Value.Metadata.Name}", p);
                    var tagInput = _HtmlGenerator.GenerateCheckBox(ViewContext, expression.ModelExplorer, expression.Name, (bool)expression.Model, new { @class = "form-check-input" });
                    tagTd.InnerHtml.SetHtmlContent(tagInput);

                }
                else if (Selects.TryGetValue(kv.Key, out var selectItems))
                {
                    var expression = new ModelExpression($"Items[{i}].{kv.Value.Metadata.Name}", p);
                    var tagInput = _HtmlGenerator.GenerateSelect(ViewContext, expression.ModelExplorer, null, expression.Name, selectItems, false, new { @class = "form-select" });
                    tagTd.InnerHtml.AppendHtml(tagInput);
                }
                else
                {

                    var expression = new ModelExpression($"Items[{i}].{kv.Value.Metadata.Name}", p);
                    kv.Value.GetHtmlAttributes(out var htmlAttributes, out var format);

                    var tagInput = _HtmlGenerator.GenerateTextBox(ViewContext, expression.ModelExplorer, expression.Name, expression.Model, format, htmlAttributes);
                    tagInput.AddCssClass("form-control");
                    tagTd.InnerHtml.SetHtmlContent(tagInput);
                }
            }

        }


    }
}

